package com.dcbeida.activity.sales;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.bean.Drug;
import com.dcbeida.bean.DrugInfo;
import com.dcbeida.bean.DrugListBean;
import com.dcbeida.bean.PatientList;
import com.dcbeida.bean.Result_Api;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.InventoryModel;
import com.dcbeida.model.SotresalesModel;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class StoresalesPresenter implements StoresalesContract.Present {


    private StoresalesContract.View view;

    private List<DrugInfo> drugInfoList;

    private InventoryModel model2;

    private SotresalesModel model;


    private Activity act;

    public StoresalesPresenter(StoresalesContract.View view, Activity act) {
        //我这里直接把activity作为view，所以不需要
        //mView.setPresenter(this);
        this.view = view;
        drugInfoList = new ArrayList<>();
        model2 = InventoryModel.newInstance();
        model = SotresalesModel.newInstance();
        this.act = act;

    }

    @Override
    public void getdrugcodeinfo(String drugcode) {
        model2.getDrugDetailDate(drugcode, new GetdrugcodeCallBack(act, DrugInfo.class, lis));
    }

    @Override
    public void getPatientInfo(int type, String keyword) {
        model.querypatient(type, keyword, new QuerypatientCallBack(act, PatientList.class,
                lis,0));
    }

    @Override
    public void getPatientInfo(int type, String keyword, int operater) {
        model.querypatient(type, keyword, new QuerypatientCallBack(act, PatientList.class,
                lis,operater));
    }

    @Override
    public void getPatientInfo2(int type, String keyword) {
        model.querypatient(type, keyword, new QuerypatientCallBack2(act, PatientList.class,
                lis));
    }

    @Override
    public void adddrugsales(String patienttruename, String patientidcardnum, String patientphonenum,
                             String amount, String invoiceimgurl,String recipepicsurl, String doctorname, int hospitalid, int cardtype) {

        String codes = "";
        int quantity = 0;
        for (DrugInfo info : drugInfoList) {
            codes += info.getDrugcode() + ",";
            quantity += info.getQuantity();
        }
        if (codes.length() > 0) {
            codes.substring(0, codes.length() - 2);
        }
        model.adddrugsales(codes, patienttruename, patientidcardnum, patientphonenum, quantity, amount, invoiceimgurl,recipepicsurl, doctorname,
                hospitalid,cardtype, new ApiCallBack(act, Result_Api.class, lis));
    }

    @Override
    public boolean isDrugInfoList() {
        if (drugInfoList.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void start() {

    }

    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model2.getCancel();
        }
    };


    int quantity;

    //根据条形码查询详细信息
    class GetdrugcodeCallBack<T> extends DialogCallback<T> {

        public GetdrugcodeCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (data instanceof DrugInfo) {
                    DrugListBean drugListBean = MyApplication.getInstance().getmDrugBean();
                    if (drugListBean != null && drugListBean.getList() != null && !drugListBean.getList().isEmpty()) {
                        boolean isExit = false;//是否存在在这个列表中
                        for (Drug drug : drugListBean.getList()) {
                            if (drug.getDrugid() == ((DrugInfo) data).getDrugid())
                                isExit = true;
                        }
                        if (!isExit) {
                            view.showTelTip("药品码错误");
                            return;
                        }
                    }
                    if (drugInfoList.contains(data)) {
                        view.showTelTip("此款药品已存在列表中...");
                        return;
                    }

                    int pos = -1;
                    int head_pos = 0;
                    for (int i = 0; i < drugInfoList.size(); i++) {
                        if (drugInfoList.get(i).getDrugid() == ((DrugInfo) data).getDrugid()) {
                            if (drugInfoList.get(i).isFirst()) {
                                head_pos = i;
                            }
                            pos = i;
                        }
                    }

                    if (pos == -1) {
                        ((DrugInfo) data).setFirst(true);
                        ((DrugInfo) data).setCount(((DrugInfo) data).getQuantity());
                        pos = drugInfoList.size();
                        head_pos = pos;
                    } else {
                        ((DrugInfo) data).setFirst(false);
                        DrugInfo headInfo = drugInfoList.get(head_pos);
                        int count = headInfo.getCount() + ((DrugInfo) data).getQuantity();
                        headInfo.setCount(count);
                        pos = pos + 1;
                        //additem(pos,data);
                    }

                    drugInfoList.add(pos, (DrugInfo) data);
                    //iew.addItem((DrugInfo) data);
                    view.addItem(pos, (DrugInfo) data, head_pos, drugInfoList.get(head_pos));

                }
            } else {
                // view.showTelTip("没有查询到此药品");
            }
        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTelTip("数据未请求成功,请检查网络...");


        }
    }


    //药店销售-查询患者信息列表
    class QuerypatientCallBack<T> extends DialogCallback<T> {

        private int operater;
        public QuerypatientCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener,int operater) {
            super(activity, type, listener);
            this.operater= operater;
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (data instanceof PatientList) {
                    if (((PatientList) data).getList().size() > 0) {
                        if(operater == 0){
                            view.showPatientlist(((PatientList) data).getList());
                        }else {
                            view.patientResult(((PatientList) data).getList(),operater);
                        }
                    } else {
                        if(operater == 0){
                            view.showAddNewPatient();
                        }else {
                            view.unPatientResult(operater);
                        }

                    }
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTelTip("数据未请求成功,请检查网络...");


        }
    }

    //药店销售-查询患者信息列表
    class QuerypatientCallBack2<T> extends DialogCallback<T> {

        public QuerypatientCallBack2(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (data instanceof PatientList) {
                    if (((PatientList) data).getList().size() > 0) {
                        view.showPatient(((PatientList) data).getList().get(0));
                    } else {
                        view.showTelTip("未查询到患者");
                    }
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTelTip("数据未请求成功,请检查网络...");


        }
    }


    //药店销售-新增销售记录 返回结果
    class ApiCallBack<T> extends DialogCallback<T> {

        public ApiCallBack(Activity activity, Class<T> tclass, DialogInterface.OnCancelListener listener) {
            super(activity, tclass, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (((Result_Api) data).getCode() == 1) {
                    view.showTelTip(((Result_Api) data).getMsg());
                    view.confimSuccess();
                }
            }
        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            //view.showTip("数据未请求成功,请检查网络...");

        }
    }

}
